//
//  BLTimeAxis+Appearance.m
//  BLTimeAxis
//
//  Created by dg on 2025/4/18.
//

#import "BLTimeAxis+Appearance.h"
#import "BLTimeAxisAppearance.h"
#import "BLTimeAxisRule.h"
#import "BLTimeAxisDigitalDivision.h"
#import "BLTimeAxisBaseLine.h"
#import "BLTimeAxisBackground.h"
#import "BLTimeAxisAppearanceRecognizer.h"

@implementation BLTimeAxis (Appearance)

- (void)registeAppearanceNotification {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateAppearance) name:KBLAppearanceUpdateNotificationName object:nil];
}


- (void)resignAppearanceNotification {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:KBLAppearanceUpdateNotificationName object:nil];
}

- (NSArray *)updateAppearanceArrayWithSize:(CGSize)size {
    BLTimeAxisAppearance *appearance = [BLTimeAxisAppearance sharedAppearance];
    id <BLTimeAxisVisitor> appearanceRecognizer = [[BLTimeAxisAppearanceRecognizer alloc] initWithViewSize:size appearance:appearance];
    id<BLTimeAxisComponent> digital = (id<BLTimeAxisComponent>)[[BLTimeAxisDigitalDivision alloc] init];
    id<BLTimeAxisComponent> rule = (id<BLTimeAxisComponent>)[[BLTimeAxisRule alloc] init];
    id<BLTimeAxisComponent> baseLine = (id<BLTimeAxisComponent>)[[BLTimeAxisBaseLine alloc] init];
    id<BLTimeAxisComponent> background = (id<BLTimeAxisComponent>)[[BLTimeAxisBackground alloc] init];
    
    [rule acceptVisitor:appearanceRecognizer];
    [digital acceptVisitor:appearanceRecognizer];
    [baseLine acceptVisitor:appearanceRecognizer];
    [background acceptVisitor:appearanceRecognizer];
    // 这个顺序千万别错
    return @[background, rule, baseLine, digital];
}

- (void)updateAppearanceWithDataArray:(NSArray<BLTimeAxisData *> *)dataArray size:(CGSize)size {
    
    id <BLTimeAxisVisitor> appearanceRecognizer = [[BLTimeAxisAppearanceRecognizer alloc] initWithViewSize:size appearance:[BLTimeAxisAppearance sharedAppearance]];
    
    for (BLTimeAxisData *axisData in dataArray) {
        [axisData acceptVisitor:appearanceRecognizer];
    }
}

- (UIColor *)updateAppearanceMainBackgroundColor {
    return [[BLTimeAxisAppearance sharedAppearance] mainBackgroundColor];
}

- (Class)updateAppearanceRenderer {
    return [[BLTimeAxisAppearance sharedAppearance] rendererClass];
}

- (void)uponAppearanceForUpdateCurrentTimeIntervalFromOffset:(CGPoint)offset viewSize:(CGSize)viewSize toOptimisticOffset:(CGFloat *)opOffset optimisticViewSize:(CGFloat *)opViewSize {
    switch ([[BLTimeAxisAppearance sharedAppearance] direction]) {
        case BLAxisDirectionHorizontal:
            *opOffset = offset.x;
            *opViewSize = viewSize.width;
            break;
        case BLAxisDirectionVertical:
            *opOffset = offset.y;
            *opViewSize = viewSize.height;
            break;
    }
}

@end
